package com.maserb.monitor.web.jw_question;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwVideoEntity;
import com.maserb.monitor.entity.jwQuestion.JwQuestionItemEntity;
import com.maserb.monitor.entity.jwQuestion.JwQuestionOptionEntity;
import com.maserb.monitor.enums.JwQuestionItemTypeEnum;
import com.maserb.monitor.enums.JwQuestionOptionTypeEnum;
import com.maserb.monitor.enums.VideoTypeEnum;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwQuestion.JwQuestionItemService;
import com.maserb.monitor.service.jwQuestion.JwQuestionOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
@RequestMapping("/jwQuestionOption")
public class JwQuestionOptionController {

    @Autowired
    JwQuestionOptionService jwQuestionOptionService;

    @Autowired
    private IToolService toolService;

    @RequestMapping("/doListQuestionOption")
    @ResponseBody
    public PageResponseResult<JwQuestionOptionEntity> doListPointCategoryItem(PageRequestParam page, final String parentId) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<JwQuestionOptionEntity>>() {
            @Override
            public Page<JwQuestionOptionEntity> invoke(Pageable var1) throws Exception {
                Page<JwQuestionOptionEntity> page = jwQuestionOptionService.findAllByParentId(parentId, var1);
                for(JwQuestionOptionEntity entity:page.getContent()){
                    if(null!=entity.getType() && !"".equals(entity.getType())) {
                        try{
                            entity.setType(JwQuestionOptionTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                        }catch (NumberFormatException e){
                            entity.setType("未知");
                        }
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/getAddQuestionOption/{parentId}")
    public ModelAndView getAddQuestionOption(@PathVariable String parentId) {
        ModelAndView result = new ModelAndView();
        JwQuestionOptionTypeEnum[] type = JwQuestionOptionTypeEnum.values();
        result.addObject("type",type);
        result.addObject("parentId", parentId);
        result.setViewName("jw_question/addOption");
        return result;
    }

    @RequestMapping("/doAddQuestionOption")
    @ResponseBody
    public AjaxResult<Boolean> doAddQuestionOption(final HttpServletRequest request, JwQuestionOptionEntity entity, String parentId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
//                entity.setMode(1);
                jwQuestionOptionService.save(entity, parentId);
                return true;
            }
        });
    }

    @RequestMapping("/getEditQuestionOption/{id}")
    public ModelAndView getEditQuestionOption(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        JwQuestionOptionEntity entity = jwQuestionOptionService.findOne(id);
        result.setViewName("jw_question/option_edit");
        JwQuestionOptionTypeEnum[] type = JwQuestionOptionTypeEnum.values();
        result.addObject("type",type);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateOption")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateOption(final HttpServletRequest request, JwQuestionOptionEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                JwQuestionOptionEntity jwQuestionOptionEntity = jwQuestionOptionService.findOne(entity.getId());
                jwQuestionOptionEntity.setName(entity.getName());
                jwQuestionOptionEntity.setType(entity.getType());
                jwQuestionOptionService.save(jwQuestionOptionEntity);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteOption")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteItem(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                jwQuestionOptionService.deleteAllByKeys(keys);
                return true;
            }
        });
    }


}
